package com.wangtao.firstapp.fragments.communityfragment;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.HotRecommendAdapter;
import com.wangtao.firstapp.adapters.MyFollowsShowAdapter;
import com.wangtao.firstapp.adapters.RecyclerViewAdapter;
import com.wangtao.firstapp.modles.HotRecommendCommentData;
import com.wangtao.firstapp.modles.HotRecommendData;
import com.wangtao.firstapp.modles.MyFollowsShowData;
import com.wangtao.firstapp.utils.HttpUtils;

/**
 * 文章列表下关注动态晒单Fragment页面
 * Created by lanouhn on 16/8/31.
 */
public class MyFollowsShowFragment extends Fragment {
    private RecyclerView rv_followsarticle;
    private MyFollowsShowAdapter myFollowsShowAdapter;// 二级页面适配器
    private MyFollowsShowData myFollowsShowData;
    // private SwipeRefreshLayout swiperefreshlayout_myfollows;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.followsempty, null);
        rv_followsarticle = (RecyclerView) view.findViewById(R.id.rv_followsarticle);
        // swiperefreshlayout_myfollows = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout_myfollows);
        initData();
        // SwipeToRefresh();
        return view;
    }


    private void initData() {

        String url = "http://open4.bantangapp.com/community/post/myAttentionPost";
        String params = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=10";
        try {
            HttpUtils.doPostAsyn(url, params, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    myFollowsShowData = gson.fromJson(result, MyFollowsShowData.class);
                    if (myFollowsShowData.getData().getList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    // Toast.makeText(getActivity(), "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    // LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()) {
                            @Override
                            public boolean canScrollVertically() {
                                return false;
                            }
                        };
                    rv_followsarticle.setLayoutManager(linearLayoutManager);
                    myFollowsShowAdapter = new MyFollowsShowAdapter(getActivity(), myFollowsShowData);
                    rv_followsarticle.setAdapter(myFollowsShowAdapter);
                    break;
            }
        }
    };
    // 下拉刷新
//    private void SwipeToRefresh() {
//        swiperefreshlayout_myfollows.setColorSchemeColors(Color.CYAN, Color.BLUE, Color.GREEN, Color.YELLOW);
//        swiperefreshlayout_myfollows.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                swiperefreshlayout_myfollows.setRefreshing(true);
//                (new Handler()).postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        swiperefreshlayout_myfollows.setRefreshing(false);
//                        myFollowsShowAdapter.notifyDataSetChanged();
//                    }
//                }, 3000);
//            }
//        });
//    }
}
